<html>
<head><meta charset="utf-8"><title>Identity match optimization · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html">Identity match optimization</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204829369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204829369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204829369">(Jul 23 2020 at 17:45)</a>:</h4>
<p>Playing around with different snippets of code, I found these that are somewhat interesting:<br>
<a href="https://rust.godbolt.org/z/EYW9na">https://rust.godbolt.org/z/EYW9na</a> </p>
<ul>
<li>I would have expected SimplifyArmIdentity to optimize the MIR to a move</li>
<li>LLVM correctly optimized this to a move</li>
</ul>
<p><a href="https://rust.godbolt.org/z/Mc8Ksa">https://rust.godbolt.org/z/Mc8Ksa</a></p>
<ul>
<li>This seems to be a more complex variant of the first snippet. When in the Some(_) variant, x.unwrap() should be the same value as the variant value.</li>
<li>MIR is not optimized to a move</li>
<li>Interestingly, LLVM does not optimize this to a move. It would be interesting if we could MIR-opt this so that we could help LLVM</li>
</ul>
<p>Thoughts on the above?</p>



<a name="204830141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204830141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204830141">(Jul 23 2020 at 17:50)</a>:</h4>
<blockquote>
<p>I would have expected SimplifyArmIdentity to optimize the MIR to a move</p>
</blockquote>
<p>I rewrote a lot of that pass to try to get it working without depending on the copy propagation pass which is unlikely to be stabilized anytime soon. I wonder if one of the other <code>mir-opt-level &gt;= 2</code> passes is causing it to not trigger?</p>
<blockquote>
<p>This seems to be a more complex variant of the first snippet. When in the Some(_) variant, x.unwrap() should be the same value as the variant value.</p>
</blockquote>
<p>I'm surprised <code>unwrap</code> isn't getting inlined. I would think it's a good candidate.</p>



<a name="204831945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204831945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204831945">(Jul 23 2020 at 18:03)</a>:</h4>
<blockquote>
<p>I wonder if one of the other mir-opt-level &gt;= 2 passes is causing it to not trigger?</p>
</blockquote>
<p>How would one check this? Does a parameter exist to enable/disable passes? Write a MIR-opt test?</p>
<blockquote>
<p>I would think it's a good candidate.</p>
</blockquote>
<p>Is debug logs the easiest way to verify why this did not fire?</p>



<a name="204832326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204832326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204832326">(Jul 23 2020 at 18:06)</a>:</h4>
<blockquote>
<p>How would one check this? Does a parameter exist to enable/disable passes? Write a MIR-opt test?</p>
</blockquote>
<p>The "easiest" thing to do would probably be to build rustc locally and remove <a href="https://github.com/rust-lang/rust/blob/39a295f52637817ba8584cb9bcebef91fd0a9f4f/src/librustc_mir/transform/simplify_try.rs#L369">this check</a> to see if it works without <code>-Zmir-opt-level=3</code></p>



<a name="204832363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204832363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204832363">(Jul 23 2020 at 18:06)</a>:</h4>
<blockquote>
<p>Is debug logs the easiest way to verify why this did not fire?</p>
</blockquote>
<p>Yeah, in particular <code>RUSTC_LOG=rustc_mir::transform::inline</code> should give you what you need</p>



<a name="204832404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204832404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204832404">(Jul 23 2020 at 18:06)</a>:</h4>
<p>er the last part might be <code>Inline</code> or <code>inliner</code> I forget</p>



<a name="204833128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204833128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204833128">(Jul 23 2020 at 18:13)</a>:</h4>
<blockquote>
<p><a href="https://rust.godbolt.org/z/EYW9na">https://rust.godbolt.org/z/EYW9na</a></p>
</blockquote>
<p>So I checked all variants {0,1,2,3} of -Zmir-opt-level and all included a discriminant check in the final MIR. It is reasonable to have this eliminated, right? Am I understanding it correctly that that would be the job of SimplifyArmIdentity?</p>



<a name="204833217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204833217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204833217">(Jul 23 2020 at 18:13)</a>:</h4>
<p>I think you're right</p>



<a name="204833236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204833236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204833236">(Jul 23 2020 at 18:14)</a>:</h4>
<p>I recall even writing a test similar to this</p>



<a name="204833346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204833346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204833346">(Jul 23 2020 at 18:14)</a>:</h4>
<p>Yeah <a href="https://github.com/rust-lang/rust/blob/39a295f52637817ba8584cb9bcebef91fd0a9f4f/src/test/mir-opt/simplify-arm.rs">https://github.com/rust-lang/rust/blob/39a295f52637817ba8584cb9bcebef91fd0a9f4f/src/test/mir-opt/simplify-arm.rs</a></p>



<a name="204833637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204833637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204833637">(Jul 23 2020 at 18:16)</a>:</h4>
<p>Oh no, looks like this didn't work then either <a href="https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-835a5cded6e7812bdecc1a577fcefe29">https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-835a5cded6e7812bdecc1a577fcefe29</a></p>



<a name="204833910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204833910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204833910">(Jul 23 2020 at 18:18)</a>:</h4>
<p>I guess the pass needs some additional logic to understand that <code>discriminant(_0) = 0</code> is the same as <code>_0 = move _1;</code> for the <code>None</code> case</p>



<a name="204836487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204836487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204836487">(Jul 23 2020 at 18:36)</a>:</h4>
<p>I could take a look into it. Do you have any idea/pointers on the top of your head that could help me, or should I just dive into it?</p>



<a name="204836937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204836937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204836937">(Jul 23 2020 at 18:39)</a>:</h4>
<p>Without having thought a lot about it, it seems like there's probably two approaches you could take:</p>
<ul>
<li>
<p>Make <code>SimplifyArmIdentity</code> do this substitution. It's already the more complex pass so it probably makes sense to do this in that pass.</p>
</li>
<li>
<p>Make <code>SimplifyBranchSame</code> consider this to be the same. That pass is pretty simple so I think the first option is better (but if you find doing is this way is easier for some reason, that's probably fine).</p>
</li>
</ul>



<a name="204837065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204837065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204837065">(Jul 23 2020 at 18:40)</a>:</h4>
<p>I'm pulling up that code to see if I have any other suggestions</p>



<a name="204837253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204837253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204837253">(Jul 23 2020 at 18:41)</a>:</h4>
<p>Actually</p>



<a name="204837272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204837272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204837272">(Jul 23 2020 at 18:41)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/master/src/librustc_mir/transform/simplify_try.rs#L184">https://github.com/rust-lang/rust/blob/master/src/librustc_mir/transform/simplify_try.rs#L184</a></p>



<a name="204837414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204837414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204837414">(Jul 23 2020 at 18:42)</a>:</h4>
<p>I wonder if it isn't triggering because it doesn't see a statement like this <a href="https://github.com/rust-lang/rust/blob/master/src/librustc_mir/transform/simplify_try.rs#L188">https://github.com/rust-lang/rust/blob/master/src/librustc_mir/transform/simplify_try.rs#L188</a></p>



<a name="204837456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204837456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204837456">(Jul 23 2020 at 18:43)</a>:</h4>
<p>If that's true, then you just need to make matching that statement optional.</p>



<a name="204838051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204838051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204838051">(Jul 23 2020 at 18:47)</a>:</h4>
<p>Could be! I don't have rustc locally yet, so I might first take a look at this in the weekend</p>



<a name="204838103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204838103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204838103">(Jul 23 2020 at 18:47)</a>:</h4>
<p>Sounds good! Feel free to ask questions if you get stuck <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="204848826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204848826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204848826">(Jul 23 2020 at 20:19)</a>:</h4>
<p>So to get started I ran <code>./build/x86_64-unknown-linux-gnu/stage1/bin/rustc src/test/mir-opt/simplify-arm.rs -Zmir-opt-level=3 -Zdump-mir=id</code>. In the mir_dump folder I see SimplifyArmIdentity has run once. In it, it does some removal of a discriminant (same as <a href="https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-ab0af5b1ef4e6c28783346a47901e771R29">https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-ab0af5b1ef4e6c28783346a47901e771R29</a> ). However there is still a discriminant left. Is that remaining discriminant to be removed in this pass? I then ran <code>RUSTC_LOG=rustc_mir::transform::simplify_try ./build/x86_64-unknown-linux-gnu/stage1/bin/rustc src/test/mir-opt/simplify-arm.rs -Zmir-opt-level=3 2&gt;log</code> to check whether the pass would run again. I could only find the single pass of it</p>



<a name="204849121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204849121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204849121">(Jul 23 2020 at 20:22)</a>:</h4>
<p>So the pass does remove something and traces<code>SUCCESS: optimization applies!</code>. Would it make sense to check why it does not remove the discriminant in the current pass, or is that on purpose, and it actually has to be removed in another pass?</p>



<a name="204849343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204849343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204849343">(Jul 23 2020 at 20:24)</a>:</h4>
<blockquote>
<p>Is that remaining discriminant to be removed in this pass?</p>
</blockquote>
<p>Yeah, that's the goal but it isn't working right now. </p>
<blockquote>
<p>I could only find the single pass of it</p>
</blockquote>
<p>I think what's probably happening is that this <a href="https://github.com/rust-lang/rust/blob/39a295f52637817ba8584cb9bcebef91fd0a9f4f/src/librustc_mir/transform/simplify_try.rs#L202">https://github.com/rust-lang/rust/blob/39a295f52637817ba8584cb9bcebef91fd0a9f4f/src/librustc_mir/transform/simplify_try.rs#L202</a> is returning <code>None</code> which the <code>?</code> means that <code>get_arm_identity_info</code> returns <code>None</code> and so the pass doesn't trigger a second time.</p>



<a name="204852800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204852800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204852800">(Jul 23 2020 at 20:58)</a>:</h4>
<p>I added traces for the output of match_get_variant_field and match_set_variant_field. They never actually return None, so I guess it is something else that doesn't match the structure. The feedback loop for this is pretty slow - do you have experience attaching a debugger to rustc when running a test?</p>



<a name="204853124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204853124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204853124">(Jul 23 2020 at 21:01)</a>:</h4>
<p>I've generally not found it to be pleasant because rustc is too slow to use without optimizations and that makes debugging hard.</p>



<a name="204853140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204853140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204853140">(Jul 23 2020 at 21:01)</a>:</h4>
<p>Are you using the <code>-i</code> incremental build option?</p>



<a name="204853151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204853151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204853151">(Jul 23 2020 at 21:01)</a>:</h4>
<p>You also only need a stage 1 build.</p>



<a name="204853226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204853226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204853226">(Jul 23 2020 at 21:02)</a>:</h4>
<p>I usually use <code>./x.py build --stage 1 -i src/libstd</code> when working on MIR stuff and that cuts the compilation time to 3-4 minutes.</p>



<a name="204853827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204853827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204853827">(Jul 23 2020 at 21:08)</a>:</h4>
<p>I didn't but i'll give it a go :) Is SimplifyArmIdentity really the only pass needed to completely remove any switchInt and discriminant in <a href="https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-ab0af5b1ef4e6c28783346a47901e771R15">https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-ab0af5b1ef4e6c28783346a47901e771R15</a> ? I don't understand the current pass, but is it currently tuned to see that a switch on a discriminant, where all arms just evaluate to their corresponding discriminant values? Along the lines you mentioned about logic that sees that discriminant(_0) = 0 is the same as _0 = move _1; for the None case</p>



<a name="204854910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204854910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204854910">(Jul 23 2020 at 21:21)</a>:</h4>
<blockquote>
<p>Is SimplifyArmIdentity really the only pass needed to completely remove any switchInt and discriminant in <a href="https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-ab0af5b1ef4e6c28783346a47901e771R15">https://github.com/rust-lang/rust/commit/6de6d70ae0a21b779d63d885438c7214e17e7a6d#diff-ab0af5b1ef4e6c28783346a47901e771R15</a> ?</p>
</blockquote>
<p>Should be yeah. Basically this pass just looks for blocks that copy all of an enum variant to a different local. What is doesn't seem to handle is when that variant has no fields so all we have to do is set the discriminant.</p>



<a name="204854993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204854993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204854993">(Jul 23 2020 at 21:22)</a>:</h4>
<p>The other pass is the one that looks for switches where all of the target blocks have equal bodies and then replaces the switch with a goto for one of the bodies.</p>



<a name="204963374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204963374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204963374">(Jul 24 2020 at 20:34)</a>:</h4>
<p>What is the overhead of running a MIR-opt pass? To me, it would be simpler to have a simple pass that turns <code>discriminant(PLACE) = VAR_INDEX;</code> into <code>PLACE = move VAR_INDEX;</code> iff VAR_INDEX is a field-less variant of the enum pointed to by PLACE.</p>



<a name="204963769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204963769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204963769">(Jul 24 2020 at 20:38)</a>:</h4>
<p>In general, there's not a lot of overhead.</p>



<a name="204964458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204964458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204964458">(Jul 24 2020 at 20:44)</a>:</h4>
<p>Hmm, I was kind of afraid of mixing the simpler logic of the fieldless enum discriminant with the more complex, existing logic. But maybe I can just add a specialised check for the fieldless enum discriminant before this <a href="https://github.com/rust-lang/rust/blob/39a295f52637817ba8584cb9bcebef91fd0a9f4f/src/librustc_mir/transform/simplify_try.rs#L378">https://github.com/rust-lang/rust/blob/39a295f52637817ba8584cb9bcebef91fd0a9f4f/src/librustc_mir/transform/simplify_try.rs#L378</a> ?</p>



<a name="204964561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/204964561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#204964561">(Jul 24 2020 at 20:44)</a>:</h4>
<p>So the two optimization variants are somewhat separated</p>



<a name="205009744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/205009744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#205009744">(Jul 25 2020 at 16:03)</a>:</h4>
<p><span class="user-mention" data-user-id="125250">@Wesley Wiser</span> I added a draft PR here <a href="https://github.com/rust-lang/rust/pull/74748">https://github.com/rust-lang/rust/pull/74748</a> . It's a draft as it has errors when building core. Any ideas?<br>
It does seem to work though. I tested it by making it run only at level 3, which did allow SimplifyBranchSame and later passes to eliminate the match</p>



<a name="205025386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/205025386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#205025386">(Jul 25 2020 at 22:57)</a>:</h4>
<p>PR errors should be fixed now and is ready for first review</p>



<a name="205126404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/205126404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#205126404">(Jul 27 2020 at 14:49)</a>:</h4>
<p>Thanks <span class="user-mention" data-user-id="139182">@Simon Vandel Sillesen</span>! I will try to leave a review tonight.</p>



<a name="205168383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Identity%20match%20optimization/near/205168383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Identity.20match.20optimization.html#205168383">(Jul 27 2020 at 21:07)</a>:</h4>
<p>(deleted)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>